Skip to content

LangChain StructuredTool #796 - 10#809

Closed
adk-bot wants to merge 2 commits into
mainfrom
agent-changes-20251022-211025
Closed

LangChain StructuredTool #796 - 10#809
adk-bot wants to merge 2 commits into
mainfrom
agent-changes-20251022-211025

Conversation

@adk-bot

@adk-bot adk-bot commented Oct 22, 2025

Copy link
Copy Markdown
Collaborator

This pull request adds an example for using LangChain's StructuredTool with ADK, as requested in issue #796.

@joefernandez joefernandez changed the title Update ADK doc according to issue #796 - 10 Update ADK doc according to issue #796 - 10 - LangChain StructuredTool Nov 4, 2025
@jcwriter74

jcwriter74 commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Docstrings added below

from google.adk.agents.llm_agent import Agent  # Import the core Agent class to create the AI assistant
from google.adk.tools.langchain_tool import LangchainTool  # Import wrapper to make LangChain tools compatible with ADK
from langchain_core.tools import tool  # Import decorator to quickly turn functions into LangChain tools
from langchain_core.tools.structured import StructuredTool  # Import class for creating tools with explicit schemas
from pydantic import BaseModel  # Import BaseModel to define structured data schemas for tool inputs


async def add(x: int, y: int) -> int:
    """
    An asynchronous function that performs addition.
    
    Args:
        x (int): The first number.
        y (int): The second number.
        
    Returns:
        int: The sum of x and y.
    """
    return x + y  # Returns the result of adding x and y


@tool
def minus(x: int, y: int) -> int:
    """
    A synchronous function decorated as a LangChain tool to perform subtraction.
    
    The @tool decorator automatically converts this function into a LangChain tool 
    using the function name as the tool name and this docstring as the description.
    
    Args:
        x (int): The number to subtract from.
        y (int): The number to be subtracted.
        
    Returns:
        int: The difference between x and y.
    """
    return x - y  # Returns the result of subtracting y from x


class AddSchema(BaseModel):
    """
    Pydantic schema defining the input structure for the 'add' tool.
    This helps the LLM understand that 'x' and 'y' must be integers.
    """
    x: int  # Defines 'x' as a required integer
    y: int  # Defines 'y' as a required integer


class MinusSchema(BaseModel):
    """
    Pydantic schema defining the input structure for the 'minus' tool.
    Ensures the LLM provides the correct types when calling the subtraction tool.
    """
    x: int  # Defines 'x' as a required integer
    y: int  # Defines 'y' as a required integer


# Create a formal 'StructuredTool' from the 'add' function.
# This method is more explicit than the @tool decorator, allowing for manual naming and schema binding.
test_langchain_add_tool = StructuredTool.from_function(
    func=add,                          # The actual logic (the add function defined above)
    name="add",                        # The name the LLM will see for this tool
    description="Adds two numbers",    # Description used by the LLM to decide when to use this tool
    args_schema=AddSchema,             # Links the Pydantic schema to validate and describe the inputs
)

# Initialize the Root Agent (the "brain" of the application).
root_agent = Agent(
    model="gemini-2.0-flash-001",      # Specifies the Google Gemini model to power the agent
    name="test_app",                   # Internal identifier/name for the agent
    description="A helpful assistant for user questions.",  # High-level description of the agent's purpose
    instruction=(                      # The system prompt that guides the agent's behavior
        "You are a helpful assistant for user questions, you have access to a"
        " tool that adds two numbers."
    ),
    tools=[                            # List of tools the agent is allowed to use
        # Wraps the StructuredTool 'add' into the ADK format
        LangchainTool(tool=test_langchain_add_tool), 
        # Wraps the decorated '@tool' function 'minus' into the ADK format
        LangchainTool(tool=minus),
    ],
)

@jcwriter74 jcwriter74 self-assigned this Jun 9, 2026
@jcwriter74

jcwriter74 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Piece of code added to show the docstrings. The source (docs/tools/third-party/index.md) for comparing whether the docstrings were the new ones or not, is not available.

Awaiting further instructions . . . @joefernandez

@jcwriter74 jcwriter74 requested a review from joefernandez June 11, 2026 22:09
Example for using LangChain's StructuredTool with ADK
@netlify

netlify Bot commented Jun 19, 2026

Copy link
Copy Markdown

Deploy Preview for adk-docs-preview ready!

Name Link
🔨 Latest commit d9824a0
🔍 Latest deploy log https://app.netlify.com/projects/adk-docs-preview/deploys/6a35634e1a0acc00081692e6
😎 Deploy Preview https://deploy-preview-809--adk-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@jcwriter74

Copy link
Copy Markdown
Collaborator

@jcwriter74 jcwriter74 closed this Jun 19, 2026
@jcwriter74 jcwriter74 changed the title Update ADK doc according to issue #796 - 10 - LangChain StructuredTool LangChain StructuredTool #796 - 10 Jun 24, 2026
@jcwriter74

jcwriter74 commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Follow up, after having closed this 809 pr, in the NEW NEXT 1882 pr

  • Quite important, given that some file paths have been removed from the current main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants